<?php

namespace App\Http\Controllers;

use App\Http\Service\Baoliao\BaoliaopostService;
use App\Http\Service\Baoliao\BaoliaoService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Productcategory;
use App\Models\Productcategoryparent;
class BaoliaopostController extends BaseController
{
    private $getRules=[
            'type'=> 'required',
            //'city_code'=>'required',
        ];
    private  $getRulesMessage=[
            'type.required'=> 'type不能为空',
           // 'city_code.required'=> 'city_code不能为空',

        ];
    public $repository;
    public function __construct(BaoliaopostService $repository,BaoliaoService $baoliaoService,Productcategory $productcategory,Productcategoryparent $productcategoryparent)
    {
        parent::__construct();
        $this->repository = $repository;
        $this->baoliaoService=$baoliaoService;
        $this->productcategory=$productcategory;
        $this->productcategoryparent=$productcategoryparent;
    }
    //爆料推送接口-备份
    public function baoliaoPushbak(Request $request)
    {
        $payload=$request->json("payload");
        //单价
        $unit_price=$payload['price'];
        //佣金比例
        $commission_rate=$payload['moneyRate'];
        //数量
        $quantity=$payload['order_num'];
        //是否是京东非自营
        $is_owner=$payload['isOwner'];
        //标识
        $platform_name=$payload['buy_platform'];
        if(!$quantity){
            $quantity=1;
        }
        //下单价=单价*数量
        $final_price=$unit_price*$quantity;
        $product_id=$payload['product_id'];
        #单个佣金=单价*佣金比例
        $commission_amount=$unit_price*($commission_rate/100);
        
        // 京东非自营打 90%
        if(!$is_owner && $platform_name=='京东' ){
            $commission_amount = $commission_amount*0.9;

        }
        //单个佣金
        $commission_amount = _round($commission_amount);
        //佣金总价
        $commission_amount_total= $commission_amount*$quantity;
        $commission_amount_total=_round($commission_amount_total);
        //获取分类id
        //platform_name_product_id($platform_name,$product_id)
        $arr=[];
        $product_unique_id=0;

        if($product_id){
            $ar=platform_name_product_id($payload['buy_platform'],$product_id);
            $product_unique_id=$ar['1'];

        }else{
            $product_id=0;
        }

        


        $cover=$payload['productImg'];
        if(is_array($cover)){
            if(!empty($cover)){
                $cover=$cover[0];
            }else{
                $cover='';
            }
        }
        
        if($payload['begin_date']=='0000-00-00 00:00:00'){
            $payload['begin_date']='1970-01-01 00:00:00';

        }

        $original_brand_name="";
        if(isset($payload['original_brand_name'])){
            $original_brand_name=$payload['original_brand_name'];
        }

        $standard_brand_name="";
        if(isset($payload['standard_brand_name'])){
            $standard_brand_name=$payload['standard_brand_name'];
        }

        $original_category_name="";
        if(isset($payload['original_category_name'])){
            $original_category_name=$payload['original_category_name'];
        }

        $standard_category_name="";
        if(isset($payload['standard_category_name'])){
            $standard_category_name=$payload['standard_category_name'];
        }




       // dd($payload['buy_fang_an']);
        $data=[
            'unique_id'=>$payload['id'],
            'article_url'=>$payload['article_url'],
            'channel'=>$request->json("channel"),
            'user_id'=>'900',
            'title'=>$payload['title'],
            'content'=>$payload['ai_text'],
            'y_content'=>$payload['bao_liao_title'],
            'cover'=>$cover,
            'begin_time'=>$payload['begin_date'],
            'purchase_steps'=>$payload['buy_fang_an'],//活动，优惠，加购，凑单，下单
            'purchase_screenshots'=>$payload['comment_img'],
            'platform_name'=>$payload['buy_platform'],
            'shop_buy_url'=>$payload['shop_buy_url'],
            'brand_name'=>$payload['smdzdm_brand_name'],
            'is_owner'=>$is_owner,
            'product_id'=>$product_id,
            'product_unique_id'=>$product_unique_id,
            'unit_price'=>$unit_price,
            'quantity'=>$quantity,//数量
            'final_price'=>$final_price,//下单价
            'commission_rate'=>$commission_rate,//佣金比例
            'commission_amount'=>$commission_amount,//佣金金额
            'commission_amount_total'=>$commission_amount_total,//佣金总金额
            'like_count'=>0,
            'collect_count'=>0,
            'comment_count'=>0,
            'view_count'=>0,
            'type'=>1, //python 抓取数据
            'publish_time'=>$payload['update_time'],
            'is_product'=>$payload['is_product'],
            'product_list'=>$payload['product_list'],
            'standard_category_name'=>$standard_category_name,
            'standard_brand_name'=>$standard_brand_name,
            'original_category_name'=>$original_category_name,
            'original_brand_name'=>$original_brand_name,

        ];
        //活动，优惠，加购，凑单，下单
        $purchase_steps=$data['purchase_steps'];

        //加购
        $purchase_steps[]=[
            "data-dimension122"=> "加购",
            "tag_text"=> "下单{$quantity}件-{$payload['title']}",
            "you_hui_tiao_zhuan_link_end"=> "",
            "huo_dong_time"=> null,
            "platform"=>$platform_name,
            "title"=> "加购",
            "description"=> "下单{$quantity}件-{$payload['title']}",
            "link"=>$payload['product_list'][0]['text']
        ];
        //下单
        if($quantity>1){

            $lastunitPrice = ($unit_price-$commission_amount_total)/$quantity;
            $purchase_steps[]=[
                "data-dimension122"=> "下单",
                "title"=> "下单",
                "description"=> "下单{$quantity}件实付{$unit_price}，下单后返利约{$commission_amount_total}元，单价合{$lastunitPrice}元",
                "link"=>'',
                "platform"=>'',
            ];
        }else{
            $purchase_steps[]=[
                "data-dimension122"=> "下单",
                "title"=> "下单",
                "description"=> "实付{$final_price}，下单后返利约{$commission_amount_total}元",
                "link"=>'',
                "platform"=>'',
            ];
        }
        //dd($data['product_list']);
        ///dd($data);
        $data['purchase_steps'] = json_encode($purchase_steps, JSON_UNESCAPED_UNICODE);
        $data['purchase_screenshots'] = json_encode($data['purchase_screenshots'], JSON_UNESCAPED_UNICODE);
        $data['product_list'] = json_encode($data['product_list'], JSON_UNESCAPED_UNICODE);
        //dd($data);        
        $this->repository->baoliaoPushbak($request,$data);
    }
    /**
     *  爆料推送消息-baoliao_post
     *  
     * @param Request $request
     * @return JsonResponse
     */
    public function addData(Request $request)
    {
        $payload=$request->json("payload");
        //单价
        $unit_price=$payload['price'];
        //佣金比例
        $commission_rate=$payload['moneyRate'];
        //数量
        $quantity=$payload['order_num'];
        //是否是京东非自营
        $is_owner=$payload['isOwner'];
        //标识
        $platform_name=$payload['buy_platform'];
        if(!$quantity){
            $quantity=1;
        }
        //下单价=单价*数量
        $final_price=$unit_price*$quantity;
        $product_id=$payload['product_id'];
        #单个佣金=单价*佣金比例
        $commission_amount=$unit_price*($commission_rate/100);
        
        // 京东非自营打 90%
        if(!$is_owner && $platform_name=='京东' ){
            $commission_amount = $commission_amount*0.9;

        }
        //单个佣金
        $commission_amount = _round($commission_amount);
        //佣金总价
        $commission_amount_total= $commission_amount*$quantity;
        $commission_amount_total=_round($commission_amount_total);
        //获取分类id
        
        $arr=[];
        $product_unique_id=0;
        if($product_id){
            $ar=platform_name_product_id($platform_name,$product_id);
            $product_unique_id=$ar['1'];
        }
       
       // dd($payload['buy_fang_an']);
        if($payload['begin_date']=='0000-00-00 00:00:00'){
            $payload['begin_date']='1970-01-01 00:00:00';

        }
        $data=[
            'unique_id'=>$payload['id'],
            'article_url'=>$payload['article_url'],
            'channel'=>$request->json("channel"),
            'user_id'=>'900',
            'title'=>$payload['title'],
            'content'=>$payload['ai_text'],
            'y_content'=>$payload['bao_liao_title'],
            'cover'=>$payload['productImg'],
            'begin_time'=>$payload['begin_date'],
            'purchase_steps'=>$payload['buy_fang_an'],//活动，优惠，加购，凑单，下单
            'purchase_screenshots'=>$payload['comment_img'],
            'platform_name'=>$payload['buy_platform'],
            'shop_buy_url'=>$payload['shop_buy_url'],
            'brand_name'=>$payload['smdzdm_brand_name'],
            'is_owner'=>$is_owner,
            'product_id'=>$product_id,
            'product_unique_id'=>$product_unique_id,
            'unit_price'=>$unit_price,
            'quantity'=>$quantity,//数量
            'final_price'=>$final_price,//下单价
            'commission_rate'=>$commission_rate,//佣金比例
            'commission_amount'=>$commission_amount,//佣金金额
            'commission_amount_total'=>$commission_amount_total,//佣金总金额
            'like_count'=>0,
            'collect_count'=>0,
            'comment_count'=>0,
            'view_count'=>0,
            'type'=>1, //python 抓取数据
            'publish_time'=>$payload['update_time'],
            'is_product'=>$payload['is_product'],
            'product_list'=>$payload['product_list'],


        ];
        //活动，优惠，加购，凑单，下单
        $purchase_steps=$data['purchase_steps'];

        //加购
        $purchase_steps[]=[
            "data-dimension122"=> "加购",
            "type" => 3,
            "tag_text"=> "下单{$quantity}件-{$payload['title']}",
            "you_hui_tiao_zhuan_link_end"=> "",
            "huo_dong_time"=> null,
            "platform"=>$platform_name,
            "title"=> "加购",
            "description"=> "下单{$quantity}件-{$payload['title']}",
            "link"=>$payload['product_list'][0]['text']
        ];
        //下单
        if($quantity>1){

            $lastunitPrice = $unit_price-$commission_amount;
            $lastunitPrice =_round($lastunitPrice);
            //dd($lastunitPrice);
            $purchase_steps[]=[
                "data-dimension122"=> "下单",
                "type"=> 5,
                "title"=> "下单",
                "description"=> "下单{$quantity}件实付{$final_price}，下单后返利约{$commission_amount_total}元，单价合{$lastunitPrice}元",
                "link"=>'',
                "platform"=>'',
            ];
        }else{
            $purchase_steps[]=[
                "data-dimension122"=> "下单",
                "type"=> 5,
                "title"=> "下单",
                "description"=> "实付{$final_price}，下单后返利约{$commission_amount_total}元",
                "link"=>'',
                "platform"=>'',
            ];
        }
        //die("sdfs");
        //dd($purchase_steps);
        ///dd($data);
        $data['purchase_steps'] = json_encode($purchase_steps, JSON_UNESCAPED_UNICODE);
        $data['purchase_screenshots'] = json_encode($data['purchase_screenshots'], JSON_UNESCAPED_UNICODE);
        $data['product_list'] = json_encode($data['product_list'], JSON_UNESCAPED_UNICODE);
        //dd($data);        
        $this->repository->addData($request,$data);
    }

    //爆料错误信息写入日志
    public function addDataLog(Request $request)
    {
        $data=[
            'type'=>$request->json("type",1),
            'platform_name'=>$request->json("platform_name"),
            'article_url'=>$request->json("article_url"),
            'unique_id'=>$request->json("unique_id"),
            'message'=>$request->json("message"),
            'type'=>$request->json("type"),
            'shop_url'=>$request->json("shop_url"),
            'product_msg_all'=>$request->json("product_msg_all"),
            'productId'=>$request->json("productId"),
            'shop_buy_url'=>$request->json("shop_buy_url"),
        ];
        $data = array_filter($data, function($value) {
            return !empty($value);
        });
        //dd($data);
        $this->repository->addDataLog($request,$data);

    }

    public function baoliaoCategoryList2(Request $request)
    {
        $data=[
            'limit'=>$request->json("limit"),
            'cursor'=>$request->json("cursor"),
            'unit_type'=>$request->json("unit_type"),
            'content'=>$request->json("content"),
            'people_id'=>$request->json("people_id"),
            'category_id'=>$request->json("category_id"),
            'name'=>$request->json("name"),
            'item_type'=>$request->json("item_type"),
            'type'=>1,

        ];
        #默认爆料
        if(empty($data['item_type'])){
            $data['item_type']=1;
        }
        #重新命名关键字name
  /*      $first=$this->productcategory->where(['id'=>$data['category_id']])->first();
        $parent_id=$first['parent_id'];
        $first2=$this->productcategoryparent->where(['parent_id'=>$parent_id])->first();
        dd($first2);*/


        $this->success($this->baoliaoService->getList($request,$data));
    }


    #爆料分类列表-
    public function baoliaoCategoryList(Request $request)
    {
        $data=[
            'limit'=>$request->json("limit"),
            'cursor'=>$request->json("cursor"),
            'unit_type'=>$request->json("unit_type"),
            'content'=>$request->json("content"),
            'people_id'=>$request->json("people_id"),
            'category_id'=>$request->json("category_id"),
            'name'=>$request->json("name"),
            'item_type'=>$request->json("item_type"),
            'type'=>1,

        ];
        #默认爆料
        if(empty($data['item_type'])){
            $data['item_type']=1;
        }
        #重新命名关键字name
/*        $parent_name='';
        $first=$this->productcategory->where(['id'=>$data['category_id']])->first();
        if($first){
            $parent_id=$first['parent_id'];
            $first2=$this->productcategoryparent->where(['parent_id'=>$parent_id])->first();
            if($first2){
                $parent_name=$first2['name'];
            }
        }

        
        if(!empty($parent_name)){
            $name="{$parent_name}/{$data['name']}";
            $data['name']=$name;
        }
        */
        //dd($data);


        $this->success($this->baoliaoService->getList($request,$data));
    }

 
    

  

}
